//
//  BN_BaseViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-3-28.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_BaseViewController.h"

@interface BN_BaseViewController () {
    BN_UserHelper *userHelper_;
}

@end

@implementation BN_BaseViewController
@synthesize userInfo;

- (void)dealloc{
    NSLog(@"父类 视图控制器 释放...");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self cancelHintView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
    /*
    if (IOS7) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }*/
    
    if (MAIN_VERSION < 7) {
        [[UIBarButtonItem appearance] setBackgroundImage: [UIImage new]                                                forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    // 剪切掉多余的背景
    switch (MAIN_VERSION) {
        case 6:
            [self.navigationController.navigationBar.layer setMasksToBounds:YES];
            break;
        default:
            break;
    }
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor whiteColor],UITextAttributeTextColor,[UIFont fontWithName:@"TrebuchetMS-Bold" size:19],UITextAttributeFont,nil]];
    
    /*UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeGesture];*/
}

#pragma mark 加载提示
- (void)viewLoadHintView {
    _loadingView = [[BN_LoadingHintView alloc]init];
    CGPoint center;
    switch (MAIN_VERSION) {
        case 7:
            if (SCREEN_HEIGHT <= 480) {
                center = CGPointMake(self.view.center.x, self.view.center.y-40);
            }else {
                center = CGPointMake(self.view.center.x, self.view.center.y);
            }
            break;
        case 6:
            if (SCREEN_HEIGHT <= 480) {
                center = CGPointMake(self.view.center.x, self.view.center.y-40-44);
            }else {
                center = CGPointMake(self.view.center.x, self.view.center.y-44);
            }
            break;
        default:
            break;
    }
    _loadingView.center = center;
    [_loadingView startLoading];
    [self.view addSubview:_loadingView];
}

- (void)cancelHintView {
    [_loadingView stopLoading];
    _loadingView = nil;
    [_loadingView removeFromSuperview];
}

#pragma mark
#pragma mark Actions

- (void)swipAction:(UISwipeGestureRecognizer *)swipeGesture {
    if (self.tabBarController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

// 清楚列表多余行
- (void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [tableView setTableFooterView:view];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    switch (self.navigationController.viewControllers.count) {
        case 1:
            return NO;
            break;
        default:
            return YES;
            break;
    }
    return YES;
}

#pragma mark 
#pragma mark 用户权限

#pragma mark 动态
- (void)navgationBarDispStyle:(float)scrollOffest {
    
    // 判断用户类型
    /*
     typedef enum
     {
     ENUM_ROLE_ADMIN = 0,            // 系统管理员
     ENUM_ROLE_LEADER = 1,           // 幼儿园园长
     ENUM_ROLE_TEACHER = 2,          // 幼儿园老师
     ENUM_ROLE_CLASS_TEACHER = 3,    // 幼儿园班主任
     ENUM_ROLE_PARENT = 4           // 家长
     }ENUM_ROLE;*/
    
    // 如果当前用户为家长的话，班级动态中不显示删除小按钮
    // 如果当前用户为老师的话，家长动态中不显示删除小按钮
    
    // 取到当前用户类型
    userInfo = [BN_UserHelper unArchiverAccountInfo];
    NSInteger userType = [userInfo.userType integerValue];
    switch (userType) {
        case ENUM_ROLE_LEADER:
        {
            if (scrollOffest == -1) {
                UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"luxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(takeVCR)];
                self.navigationItem.rightBarButtonItem = rightItem;
            }else {
                
                if (scrollOffest == 0) {
                    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"luxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(takeVCR)];
                    self.navigationItem.rightBarButtonItem = rightItem;
                }else {
                    self.navigationItem.rightBarButtonItem = nil;
                }
            }
        }
            break;
            
        case ENUM_ROLE_CLASS_TEACHER:
        {
            if (scrollOffest == -1) {
                UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"luxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(takeVCR)];
                self.navigationItem.rightBarButtonItem = rightItem;
            }else {
            
                if (scrollOffest == 0) {
                    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"luxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(takeVCR)];
                    self.navigationItem.rightBarButtonItem = rightItem;
                }else {
                    self.navigationItem.rightBarButtonItem = nil;
                }
            }
        }
            break;
        case ENUM_ROLE_PARENT:
        {
            if (scrollOffest == -1) {
                self.navigationItem.rightBarButtonItem = nil;
            }else {
                if (scrollOffest == 0) {
                    self.navigationItem.rightBarButtonItem = nil;
                }else {
                    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiazhang"] style:UIBarButtonItemStylePlain target:self action:@selector(goCreateVC)];
                    self.navigationItem.rightBarButtonItem = rightItem;
                }
            }
        }
            break;
        default:
            break;
    }
}


- (void)goCreateVC {
    // code...
}

- (void)takeVCR {
    // code...
}

#pragma mark 通知

- (void)noticeNavgationBar {
    // 权限 老师、园长能发 OR 家长不能发通知
    userInfo = [BN_UserHelper unArchiverAccountInfo];
    NSInteger userType = [userInfo.userType integerValue];
    switch (userType) {
            
        case ENUM_ROLE_LEADER:
        {
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiazhang"] style:UIBarButtonItemStylePlain target:self action:@selector(goCreateVC)];
            self.navigationItem.rightBarButtonItem = rightItem;
        }
            break;
        case ENUM_ROLE_CLASS_TEACHER:
        {
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiazhang"] style:UIBarButtonItemStylePlain target:self action:@selector(goCreateVC)];
            self.navigationItem.rightBarButtonItem = rightItem;
        }
            break;
        case ENUM_ROLE_PARENT:
        {
            self.navigationItem.rightBarButtonItem = nil;
        }
            break;
        default:
            break;
    }
}

#pragma mark 幼儿园
- (void)schoolNavgationBar:(NSInteger)section {
    userInfo = [BN_UserHelper unArchiverAccountInfo];
    NSInteger userType = [userInfo.userType integerValue];
    switch (userType) {
        case ENUM_ROLE_LEADER:
        {
            if (section != 4) {
                UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiazhang"] style:UIBarButtonItemStylePlain target:self action:@selector(goCreateVC)];
                self.navigationItem.rightBarButtonItem = rightItem;
            }else {
                self.navigationItem.rightBarButtonItem = nil;
            }
        }
            break;
        case ENUM_ROLE_CLASS_TEACHER:
        {
            self.navigationItem.rightBarButtonItem = nil;
        }
            break;
        case ENUM_ROLE_PARENT:
        {
            if (section == 4) {
                UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiazhang"] style:UIBarButtonItemStylePlain target:self action:@selector(goCreateVC)];
                self.navigationItem.rightBarButtonItem = rightItem;
            }else {
                self.navigationItem.rightBarButtonItem = nil;
            }

        }
            break;
        default:
            break;
    }
}

#pragma mark
#pragma mark 未读消息

- (void)getSectionNews:(NSInteger)section {
    if (!userHelper_) {
        userHelper_ = [[BN_UserHelper alloc]init];
    }
    __weak typeof(self) weakSelf = self;
    [userHelper_ queryNewNotificationsWithCompleteHandler:^(id result, NSString *errMsg) {
        if (errMsg) {
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
        }else{
            weakSelf.newsDict = (NSDictionary *)result;
            [weakSelf getLatestBadgeValueSection:section netNewsDict:weakSelf.newsDict];
        }
    }];
    
}

- (void)getLatestBadgeValueSection:(NSInteger)section netNewsDict:(NSDictionary *)netNewsDict {
    /*
     "postNumb":"新动态数量",
     "notiNumb":"新通知数量"，
     "schoNumb":"新幼儿园信息数量",
     "messNumb":"新通讯录信息数量"，
     "meNumb":"新我的信息数量"
     */
    switch (section) {
        case DANAMIC:
            self.tabBarItem.badgeValue = FORMAT_STR(netNewsDict[@"postNumb"]);
            break;
        case NOTICE:
            self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",netNewsDict[@"notiNumb"]];
            break;
        case CHAT:
            self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",netNewsDict[@"messNumb"]];
            break;
        case SCHOOL:
            self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",netNewsDict[@"schoNumb"]];
            break;
            
        case MINE:
            self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",netNewsDict[@"meNumb"]];
            break;
        default:
            break;
    }
}

#pragma mark 工具
/*UIDeviceOrientation 是机器硬件的当前旋转方向   这个你只能取值 不能设置
UIInterfaceOrientation 是你程序界面的当前旋转方向   这个可以设置
判断设备现在的方向*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //宣告一個UIDevice指標，並取得目前Device的狀況
    UIDevice *device = [UIDevice currentDevice] ;
    
    //取得當前Device的方向，來當作判斷敘述。（Device的方向型態為Integer）
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"螢幕朝上平躺");
            break;
            
        case UIDeviceOrientationFaceDown:
            NSLog(@"螢幕朝下平躺");
            break;
            
            //系統無法判斷目前Device的方向，有可能是斜置
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
            
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"螢幕向左橫置");
            break;
            
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"螢幕向右橫置");
            break;
            
        case UIDeviceOrientationPortrait:
            NSLog(@"螢幕直立");
            break;
            
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"螢幕直立，上下顛倒");
            break;
            
        default:
            NSLog(@"無法辨識");
            break;
    }
    
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
